﻿using System.Collections.Generic;
using System.Windows.Controls;
using System.Text.RegularExpressions;

public static class UserControlExtentsion
{
    /// <summary>获取应用中附加的参数集合。</summary>
    /// <param name="thiz"></param>
    /// <returns></returns>
    public static Dictionary<string, string> GetConfigs(this UserControl thiz)
    {
        if (thiz.Tag is Dictionary<string, string>)
        {
            return thiz.Tag as Dictionary<string, string>;
        }
        else
        {
            return new Dictionary<string, string>();
        }
    }
    /// <summary>获取应用的代码</summary>
    /// <param name="thiz"></param>
    /// <returns></returns>
    public static string GetCode(this UserControl thiz)
    {
        string name = thiz.Name;
        if (string.IsNullOrEmpty(name))
            return string.Empty;
        else
        {
            string pattern = @"\[(.+?)\]";
            var match = Regex.Match(name, pattern);
            if (match.Success)
                return match.Groups[1].Value;
            else
                return name;
        }
    }
    /// <summary>获取应用配置信息中的指定值。</summary>
    /// <param name="thiz"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string GetConfigValue(this UserControl thiz, string key)
    {
        var config = GetConfigs(thiz);

        if (config.ContainsKey(key))
            return config[key];
        else
            return string.Empty;
    }
}
